package jgl.vajra.operator.part.home.activity;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import jgl.vajra.operator.R;
import jgl.vajra.operator.bean.MyCarListBean;
import jgl.vajra.operator.databinding.ActivityListeningMyCarBinding;
import jgl.vajra.operator.event.ListeningMyCarEvent;
import jgl.vajra.operator.manager.AccountManager;
import jgl.vajra.operator.manager.IntentManager;
import jgl.vajra.operator.part.home.contract.ListeningMyCarContract;
import jgl.vajra.operator.part.home.viewModel.ListeningMyCarViewModel;
import jgl.vajra.operator.widget.databindingadapter.BaseBindingItemPresenter;
import jgl.vajra.operator.widget.databindingadapter.SingleTypeBindingAdapter;
import jgl.vajra.operator.widget.interfaces.RefreshRecyclerLoadStatusListener;
import jgl.vajra.operator.widget.mvvm.factory.CreateViewModel;
import jgl.vajra.operator.widget.mvvm.view.BaseMVVMActivity;
import jgl.vajra.operator.widget.recyclerView.RefreshRecyclerNetConfig;
import jgl.vajra.operator.widget.utils.ToastUtils;
import jgl.vajra.operator.widget.utils.UIUtils;

@CreateViewModel(ListeningMyCarViewModel.class)
public class ListeningMyCarActivity extends BaseMVVMActivity<ListeningMyCarViewModel, ActivityListeningMyCarBinding> implements ListeningMyCarContract.View, BaseBindingItemPresenter<MyCarListBean> {

    private SingleTypeBindingAdapter adapter;
    private List<MyCarListBean> dataList;
    private List<String> carList = new ArrayList<>();

    @Override
    public int getLayoutId() {
        return R.layout.activity_listening_my_car;
    }

    @Override
    public void initView() {
        super.initView();
        mBinding.setView(this);
        carList.addAll((ArrayList) getIntent().getSerializableExtra("carList"));

        mBinding.recyclerView.setRefreshRecyclerNetConfig(new RefreshRecyclerNetConfig() {
            @Override
            public Observable getNetObservable(Map<String, Object> map, int action) {
                map.put("Authorization", AccountManager.getInstance().getLoginData(UIUtils.getContext()).authorization);
                map.put("type", 2);
                return mViewModel.getMyCarList(map);
            }
        });


        adapter = new SingleTypeBindingAdapter(this, null, R.layout.item_listening_my_car);
        mBinding.recyclerView.setAdapter(adapter);
        adapter.setItemPresenter(this);
        mBinding.recyclerView.setIsDataObject(true);
        mBinding.recyclerView.setRefreshLoaderMore(false, false);
        mBinding.recyclerView.setRefreshRecyclerLoadStatusListener(new RefreshRecyclerLoadStatusListener<List<MyCarListBean>>() {
            @Override
            public void onSucceed(List<MyCarListBean> data, int action) {
                for (int i = 0; i < data.size(); i++) {
                    for (int j = 0; j < carList.size(); j++) {
                        if(data.get(i).logo.equals(carList.get(j))){
                            data.get(i).isClick=true;
                        }
                    }
                }

                dataList = data;

                mBinding.recyclerView.setRefreshLoaderMore(false, false);

                mBinding.recyclerView.requestNetObjectSuccess(data, action);
            }

            @Override
            public void onError(String errorInfo, int errorCode, int action) {
                super.onError(errorInfo, errorCode, action);
//                mBinding.recyclerView.requestNetError(errorInfo, errorCode);
            }
        });
        mBinding.recyclerView.firstLoad();
    }


    //详情
    @Override
    public void onDetails(MyCarListBean itemData) {
        IntentManager.toCarDetailsActivity(this,itemData.remark);
    }

    @Override
    public void onDetermine() {
        carList.clear();
        for (int i = 0; i < dataList.size(); i++) {
            if (dataList.get(i).isClick) {
                carList.add(dataList.get(i).logo);
            }
        }

        if (carList.size() == 0) {
            ToastUtils.showShort("请选择我的豪车");
            return;
        }
        EventBus.getDefault().post(new ListeningMyCarEvent(carList));
        finish();

    }

    @Override
    public void onItemClick(int position, MyCarListBean itemData) {
        itemData.isClick = !itemData.isClick;
        adapter.refresh();
    }
}
